package com.example.base.security.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.IsSecurity;
import com.example.base.security.domain.SysMenuEntity;
import com.example.base.security.domain.vo.SysMenuVo;
import com.example.base.security.service.SysMenuService;
import com.example.base.system.domain.SysApi;
import com.example.base.system.service.SysApiService;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@Slf4j
@Api(tags = "菜单管理模块")
@RequestMapping("sysmenu")
@RestController
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysApiService sysApiService;

    @ApiModelProperty("菜单tree模块")
    @GetMapping("tree")
    public JSONObject getTreeMenuController(){
        List<SysMenuEntity> sysMenus=sysMenuService.listTree();
        return Result.successInfo(sysMenus);
    }

    @ApiModelProperty("菜单获取一个")
    @GetMapping("getone")
    public JSONObject getOneController(@RequestParam("id")Serializable id){
        SysMenuEntity sysMenuEntity=sysMenuService.getById(id);
        if(sysMenuEntity==null){
            return Result.errorInfo("获取不到菜单信息");
        }
        List<SysApi> sysApis=sysApiService.getSysApisByMenuId(sysMenuEntity.getMenuId().toString());
        SysMenuVo sysMenuVo=new SysMenuVo();
        sysMenuVo.setApiList(sysApis);
        BeanUtils.copyProperties(sysMenuEntity,sysMenuVo);
        return Result.successInfo(sysMenuVo);
    }

    @ApiModelProperty("获取api列表")
    @GetMapping("/getapis")
    public JSONObject getApisController(@RequestParam("menuId")String id){
        if(!StringUtils.isNotBlank(id)){
            return Result.errorInfo("id不得为空");
        }
        List<SysApi> sysApis=sysApiService.getSysApisByMenuId(id);
        return Result.successInfo(sysApis);
    }

    @GetMapping("gettreeapis")
    public JSONObject getTreeApisControlelr(){
        JSONArray array=sysApiService.getTreeArray();
        return Result.successInfo(array);
    }

    @ApiModelProperty("获取角色api列表")
    @GetMapping("getapibyroleId")
    public JSONObject getApisByRoleId(@RequestParam("roleId")String roleId){
        if(!StringUtils.isNotBlank(roleId)){
            return Result.errorInfo("roleId不得为空");
        }
        List<Integer> ids=sysApiService.getApiIdsByRoleId(roleId);
        return Result.successInfo(ids);
    }



    @ApiModelProperty("菜单新增和保存")
    @PostMapping("saveorupdate")
    public JSONObject saveOrUpdateController(@Validated @RequestBody SysMenuVo vo) throws GlobalExcetion {
        SysMenuEntity entity=sysMenuService.saveOrUpdateSysMenuEntity(vo);
        return Result.successInfo("操作成功",entity.getMenuId()+"");
    }

    @ApiOperation("菜单获取模块")
    @GetMapping("getmenu")
    public JSONObject getMenuController(HttpServletRequest request){
       JSONObject data=new JSONObject();
        JSONArray dashboard=new JSONArray();
        dashboard.add("welcome");
        dashboard.add("ver");
        dashboard.add("time");
        dashboard.add("progress");
        dashboard.add("echarts");
        dashboard.add("about");
       List<SysMenuEntity> list=sysMenuService.listTreeByUser();
       data.put("menu",list);
       data.put("dashboardGrid",dashboard);
       data.put("permissions", Collections.EMPTY_LIST);
       return Result.successInfo(data);
    }


    @IsSecurity(isSecurity = false)
    @GetMapping("getlist")
    public JSONObject getList(){
        List<SysMenuEntity> list=sysMenuService.list();
        return Result.successInfo(list);
    }


}
